<?php

require_once '../application/modules/admin/controllers/SuperControllerAdmin.php';

class Admin_UsersController extends Admin_SuperController {

    public function indexAction() {
        if ($this->getRequest()->isXmlHttpRequest()) {
            $this->_helper->layout->disableLayout();
            if ($this->_request->event == 'removeRow')
                $this->onRemoweRow($this->_request->id);
            if ($this->_request->event == 'update')
                $this->onUpdate($this->getRequest()->getParams());
        }

        $this->view->order_field = isset($this->_request->order_field) ? $this->_request->order_field : 'id';
        $this->view->order_dir = isset($this->_request->order_dir) ? $this->_request->order_dir : 'DESC';

        $this->view->tableColumn = array(
            0 => array(
                'title' => 'id',
            ),
            1 => array(
                'title' => 'email',
                'field' => 'email'
            ),
            2 => array(
                'title' => 'name',
                'field' => 'name',
            ),
            6 => array(
                'title' => 'active',
                'field' => 'active'
            ),
            8 => array(
                'title' => 'Change',
            ),
        );

        $view_page = new Zend_Session_Namespace('view_apage');
        $onPage = $view_page->on_page;
        //$onPage = 4;
        $page = (int) $this->_request->page;
        if (empty($page))
            $page = 1;

        $qAr = array(
            'table_name' => 'users',
            'order_field' => $this->view->order_field,
            'order_dir' => $this->view->order_dir,
            'offset' => ($page - 1) * $onPage,
            'limit' => $onPage,
        );
        $search = '';
        if ($this->_request->email && $this->_request->event != 'update')
            $search = ' AND t.email LIKE \'%' . addslashes($this->_request->email) . '%\'';

        $CONTM = new Admin_Model_Content();
        $this->view->info = $CONTM->getItems($qAr, $count_rows, $search);
        $this->view->countRow = $count_rows;

        $paginator = Zend_Paginator::factory((int) $this->view->countRow);
        $paginator->setDefaultItemCountPerPage($onPage);
        $paginator->setCurrentPageNumber($page);

        $this->view->paginator = $paginator;
        $this->view->onPage = $onPage;
    }

    protected $sructure = array(
        0 => array('name' => 'name', 'type' => 'str', 'valid' => true,),
        1 => array('name' => 'email', 'type' => 'str',),
        4 => array('name' => 'active', 'type' => 'one-checkbox',),
        5 => array('name' => 'confirm', 'value' => 1, 'type' => 'one-checkbox',),
    );

    public function editAction() {
        Zend_Layout::getMvcInstance()->disableLayout();
        $this->_helper->viewRenderer->setNoRender();
        $id = (int) $this->_request->id;
        if (!empty($id)) {
            $CONTM = new Admin_Model_Content();
            $info = $CONTM->getOneObj('users', $id);
            if (is_array($info)) {
                $this->info = array();
                $this->info['id'] = $info['id'];
                $this->info['event'] = 'update';
                $this->info['fields'] = $this->addValue($this->sructure, $info);
                include (APPLICATION_PATH . '/modules/admin/views/scripts/snippets/lego.phtml');
            }
        } else {
            throw new Exception('no parameters required');
        }
    }

    protected function onRemoweRow($id) {
        if (!empty($id)) {
            $ACNTM = new Admin_Model_Content();
            $ACNTM->removeRow('users', $id);
        }
    }

    protected function onUpdate($data) {
        if (!empty($data)) {
            $data['active'] = isset ($data['active']) ? $data['active'] : 0;
            $data['confirm'] = isset ($data['confirm']) ? $data['confirm'] : 0;
            $ACNTM = new Admin_Model_Content();
            $ACNTM->update('users', $data);
        }
    }

}
